var express = require("express");
// 创建路由
var router = express.Router();
const OrderApi = require("../api/order");



// 获取订单列表数据
router.get('/', async function (req, res, next) {
  const { curPage, eachPage } = req.query;
  const data = await OrderApi.get({ curPage, eachPage })
  res.send(data);
});

// 新增订单
router.post('/addOrder', async function (req, res, next) {
  const { commodities, isAffirmReceive, isPaySuccess, orderTime, totalPrice, userId, shoppingCartId, searchName, isDisplay } = req.body;
  // console.log({ commodities, isAffirmReceive, isPaySuccess, orderTime, totalPrice, userId });
  const data = await OrderApi.addOrder({ commodities, isAffirmReceive, isPaySuccess, orderTime, totalPrice, userId, shoppingCartId, searchName, isDisplay })
  res.send(data);
});

// 删除订单
router.delete('/delOrder', async function (req, res, next) {
  const { _id } = req.body;
  const data = await OrderApi.delOrder(_id)
  res.send(data);
});

// 通过商品和id名称查询订单
router.get('/findOrder', async function (req, res, next) {
  // data  { name: '小' }
  const data = req.query;
  const temp = await OrderApi.findOrder(data);
  res.send(temp);
});


// 修改订单状态值
router.patch('/changeState', async function (req, res, next) {
  const { _id } = req.body;
  const data = await OrderApi.changeState(_id);
  res.send(data);
});



// 根据订单号查询订单信息  
router.get('/findOneOrderFromId', async function (req, res, next) {
  const { _id } = req.query;
  const data = await OrderApi.findOneOrderFromId(_id);
  res.send(data);
});

// 根据用户id  查找
router.get('/findOrderfromUserId', async function (req, res, next) {
  const { _id } = req.query;
  const data = await OrderApi.findOrderfromUserId(_id);
  res.send(data);
});


// 暴露接口
module.exports = router;
